import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'controllers/music_controller.dart';
import 'models/music.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: 'Music Player',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MusicPlayerPage(),
    );
  }
}

class MusicPlayerPage extends StatelessWidget {
  MusicPlayerPage({super.key});

  final List<Music> musicList = [
    Music(
        title: 'Song  1',
        artist: 'Artist 1',
        url: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3'),
    Music(
        title: 'Song 2',
        artist: 'Artist 2',
        url: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3'),
    Music(
        title: 'Song 3',
        artist: 'Artist 3',
        url: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-3.mp3'),
  ];

  @override
  Widget build(BuildContext context) {
    final MusicController musicController = Get.put(MusicController());

    return Scaffold(
      appBar: AppBar(
        title: const Text('Music Player'),
      ),
      body: Column(
        children: [
          Expanded(
            child: ListView.builder(
              itemCount: musicList.length,
              itemBuilder: (context, index) {
                return Card(
                  margin:
                      const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
                  child: ListTile(
                    leading: Icon(Icons.music_note, color: Colors.blue),
                    title: Text(musicList[index].title),
                    subtitle: Text(musicList[index].artist),
                    onTap: () {
                      musicController.playMusic(musicList[index]);
                    },
                  ),
                );
              },
            ),
          ),
          Obx(() => musicController.currentMusic.value != null
              ? Container(
                  padding: const EdgeInsets.all(16),
                  decoration: BoxDecoration(
                    color: Colors.blue.shade50,
                    borderRadius: BorderRadius.circular(8),
                  ),
                  margin: const EdgeInsets.all(16),
                  child: Column(
                    children: [
                      Text(
                        musicController.currentMusic.value!.title,
                        style: TextStyle(
                            fontSize: 20, fontWeight: FontWeight.bold),
                      ),
                      Text(
                        musicController.currentMusic.value!.artist,
                        style: TextStyle(fontSize: 16, color: Colors.grey),
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          IconButton(
                            onPressed: () => musicController.isPlaying.value
                                ? musicController.pauseMusic()
                                : musicController.playMusic(
                                    musicController.currentMusic.value!),
                            icon: Icon(
                              musicController.isPlaying.value
                                  ? Icons.pause
                                  : Icons.play_arrow,
                              size: 32,
                              color: Colors.blue,
                            ),
                          ),
                        ],
                      )
                    ],
                  ),
                )
              : Container())
        ],
      ),
    );
  }
}
